package mybatis.service.impl;

import java.util.List;
import java.util.UUID;

import mybatis.dao.UserDao;
import mybatis.entity.User;
import mybatis.service.UserService;
import mybatis.util.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	@Override
	public boolean exist(String username) {
		// TODO Auto-generated method stub
		User _user = userDao.findByName(username);
		if(_user == null) {
			return false;
		}else {
			return true;
		}
	}

	@Override
	public void regest(User user) {
		String code = UUID.randomUUID().toString().replace("-", "");
		user.setCode(code);
		// TODO Auto-generated method stub
		userDao.add(user);
		//发送邮件
		MailUtils.sendMail("mest.ondt@gmail.com", "<a href='http://localhost:8080/GameHub/user/active?code="+code+"'>请点击连接激活账户","请激活账户");
	}

	@Override
	public boolean active(String code) {
		User user = userDao.findByCode(code);
		if(user == null) {
			return false;
		}else {
			userDao.updateById(user.getId());
			return true;
		}
	}

	@Override
	public User login(String username,String password) {
		User _user = userDao.findByNameAndPassword(username,password);
		return _user;
	}

	@Override
	public void uploadHeader(int userid,String headersrc) {
		userDao.updateHeader(userid,headersrc);
	}


}
